package com.kantboot.business.ac.service.impl;

import com.kantboot.business.ac.domain.entity.BusAcVipOfYearOrder;
import com.kantboot.business.ac.repository.BusAcVipOfYearRepository;
import com.kantboot.business.ac.service.IBusAcCommonService;
import com.kantboot.functional.pay.order.module.entity.FunctionalPayOrder;
import com.kantboot.functional.pay.order.service.IFunctionalPayOrderService;
import com.kantboot.system.setting.service.ISysSettingService;
import com.kantboot.system.user.service.ISysBalanceService;
import com.kantboot.system.user.service.ISysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service
public class BusAcCommonServiceImpl implements IBusAcCommonService {

    @Resource
    private ISysSettingService settingService;

    @Resource
    private ISysBalanceService balanceService;

    @Resource
    private IFunctionalPayOrderService payOrderService;

    @Resource
    private BusAcVipOfYearRepository busAcVipOfYearOrderRepository;

    @Resource
    private ISysUserService userService;

    @Override
    public Map<String,String> getAcProjectSetting() {
        return settingService.getMapByGroupCode("acProject");
    }

    @Override
    public Double getRechargeVipOfYearPrice() {
        return Double.parseDouble(settingService.getValue("acProject", "vipPriceOfYear"));
    }

    @Override
    public Double getSelfBalanceScoreToRmb() {
        // 一元人民币可以兑换多少积分
        String value = settingService.getValue("acProject", "moneyOfOneToScore");
        // 自己的积分余额
        Double score = balanceService.getSelf().get("score");
        BigDecimal bigDecimal = new BigDecimal(score);
        BigDecimal bigDecimal1 = new BigDecimal(value);
        return bigDecimal.divide(bigDecimal1).doubleValue();
    }

    @Override
    public FunctionalPayOrder generateOrderIdOfRechargeVipOfYear() {
        String vipPriceOfYear = settingService.getValue("acProject", "vipPriceOfYear");
        // 生成订单
        FunctionalPayOrder payOrder = payOrderService.generatePayOrder(
                new FunctionalPayOrder().setAmount(Double.parseDouble(vipPriceOfYear))
                        .setProductCode("rechargeVipOfYear")
                        .setCurrency("CNY")
                        .setDescription("充值vip一年"));

        busAcVipOfYearOrderRepository.save(
                new BusAcVipOfYearOrder()
                        .setUserId(userService.getSelfId())
                        .setOrderId(payOrder.getId()));

        return payOrder;
    }
}
